package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.settlement.parameter.AdRateInfoPo;
import com.ymatou.iapi.settlement.parameter.AdRateProductInfo;
import com.ymatou.iapi.settlement.parameter.GenerateAdRateInfoReq;
import com.ymatou.iapi.settlement.service.GenerateAdRateInfoCall;
import com.ymatou.iapi.settlement.service.SettlementService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AnlihuiCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAnliOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sqlwapper.YmtAnliSettlementDetailWrapper;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 生成广告费率信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GenerateAdRateInfo {
	private static GenerateAdRateInfoReq generateAdRateInfoReq;
	private static GenerateAdRateInfoCall generateAdRateInfoCall;

	private static Long userId = 20238699L;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("生成广告费率信息接口");
	}

	@Before
	public void caseUp() {
		generateAdRateInfoReq = new GenerateAdRateInfoReq();
		generateAdRateInfoCall = new GenerateAdRateInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(generateAdRateInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void verifyDb(List<OrderDetailPo> orderDetailList) {

		try {

			long orderId = generateAdRateInfoReq.getOrderId();

			List<AdRateProductInfo> adRateProductInfoList = generateAdRateInfoReq
					.getAdProductInfos();

			List<AdRateInfoPo> adRateInfoPoList = new YmtSettlementInvoiceWapper()
					.selectYmtAdRateInfo(orderId);

			Logger.comment(String.format(
					"校验校验orderId:%s Ymt_AdRateInfo广告费率信息表", orderId));

			Logger.verifyEquals(adRateProductInfoList.size(),
					adRateInfoPoList.size(), "广告费率信息记录数");

			for (int i = 0; i < adRateInfoPoList.size(); i++) {

				// 数据库
				AdRateInfoPo adRateInfoPo = adRateInfoPoList.get(i);

				String productId = adRateInfoPo.getProductId();

				boolean isExist = adRateProductInfoList.stream().anyMatch(
						orderInfoPo -> orderInfoPo.getProductId().equals(
								productId));

				Logger.verifyEquals(true, isExist,
						String.format("校验该商品:%s存在广告费率表里", productId));

				for (AdRateProductInfo adRateProductInfo : adRateProductInfoList) {

					if (adRateProductInfo.getProductId().equals(productId)) {

						Logger.comment(String.format("校验 productId:%s 广告费率信息表",
								productId));

						Logger.verifyEquals(adRateProductInfo.getAdCostId(),
								adRateInfoPo.getAdCostId(), "校验AdId");

						Logger.verifyEquals(adRateProductInfo.getAdId(),
								adRateInfoPo.getAdId(), "校验AdCostId");

						DataUtil.verifyDecimalEquals(
								adRateProductInfo.getAdRate(),
								adRateInfoPo.getAdRate(), "校验AdRate");

						DataUtil.verifyDecimalEquals(
								adRateProductInfo.getAdCostMax(),
								adRateInfoPo.getAdPerCostMax(),
								"校验AdPerCostMax");
					}
				}

				Logger.verifyEquals(productId, adRateInfoPo.getProductId(),
						"校验ProductId");

				String orderInfoId = adRateInfoPo.getOrderInfoId();

				isExist = orderDetailList.stream().anyMatch(
						orderInfoPo -> orderInfoPo.getOrderDetailId()
								.toString().equals(orderInfoId));

				Logger.verifyEquals(true, isExist,
						String.format("校验该商品记录:%s存在广告费率表里", orderInfoId));

				orderDetailList.forEach(orderInfoPo -> {

					if (orderInfoPo.getOrderDetailId().equals(orderInfoId)) {

						BigDecimal count = new BigDecimal(orderInfoPo
								.getProductCount());

						// 同商品规格总价格 = 单商品价格 * 数量 + 运费 - 商家优惠 - 商家满减优惠
						BigDecimal price = orderInfoPo.getProductPrice()
								.multiply(count).add(orderInfoPo.getFreight())
								.subtract(orderInfoPo.getSellerCouponAmount())
								.subtract(orderInfoPo.getPromotionAmount());

						Logger.verifyEquals(orderInfoPo.getProductCount(),
								adRateInfoPo.getProductCount(),
								"校验ProductCount");

						DataUtil.verifyDecimalEquals(price,
								adRateInfoPo.getProductPrice(),
								"校验ProductPrice");

						BigDecimal calcCostAmount = price.multiply(adRateInfoPo
								.getAdRate());

						BigDecimal costAmount = BigDecimal.ZERO;

						if (DataUtil.isBiggerThan(calcCostAmount, adRateInfoPo
								.getAdPerCostMax().multiply(count))) {

							costAmount = adRateInfoPo.getAdPerCostMax()
									.multiply(count);

						} else {
							costAmount = calcCostAmount;
						}

						DataUtil.verifyDecimalEquals(costAmount,
								adRateInfoPo.getCostAmount(), "校验CostAmount");

					}

				});

				if (AnlihuiCallService.isExistSettlement(orderId)) {

					YmtAnliSettlementDetailWrapper wrapper = new YmtAnliSettlementDetailWrapper();

					BigDecimal rebateAmount = wrapper
							.selectByOrderIdAndCatalogId(orderId,
									adRateInfoPo.getCatalogId())
							.getRebateAmount();

					if (adRateInfoPo.getCostAmount().compareTo(rebateAmount) <= 0) {

						Logger.verifyEquals(true,
								adRateInfoPo.getNoNeedSettle(),
								"校验NoNeedSettle");
						adRateInfoPo.setCostAmount(BigDecimal.ZERO);
					} else
						Logger.verifyEquals(null,
								adRateInfoPo.getNoNeedSettle(),
								"校验NoNeedSettle");
				}
			}

			Map map = new HashMap();

			map.put("orderId", orderId);
			map.put("businessType", 13);

			List<YmtSettlementInvoice> ymtSettlementInvoiceList = new YmtSettlementInvoiceWapper()
					.selectYmtSettlementInvoice(map);

			BigDecimal expectSettlementAmount = adRateInfoPoList.stream()
					.filter(ad -> ad.getRelatedCancelId() == null)
					.map(ad -> ad.getCostAmount())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			DataUtil.verifyDecimalEquals(expectSettlementAmount,
					ymtSettlementInvoiceList.get(0).getCashAmount(),
					"校验实际结算的广告费用");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_001() {
		Logger.start(true, "一个订单多个商品其中一个是广告商品,广告费率信息生成成功");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			OrderDetailPo orderDetailPo = orderDetailPoList.get(0);

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderDetailPo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(
					new Random().nextFloat() / 10));

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderDetailPo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_002() {
		Logger.start(true, "一个订单多个广告商品,广告费率信息生成成功");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long tradingId = placeOrderResp.getTradingId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(), tradingId);

			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderDetailPoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(new BigDecimal(new Random()
						.nextFloat() / 10));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(orderDetailPoList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_003() {
		Logger.start(true, "一个订单同一种广告商品多个不同规格,广告费率信息生成成功");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			Order order = placeOrderResp.getOrderList().get(0);
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(), tradingId);

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			adRateProductInfo.setProductId(order.getOrderDetailPoList().get(0)
					.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(
					new Random().nextFloat() / 10));

			adRateProductInfo.setAdCostMax(adCostMax);

			adRateProductInfoList.add(adRateProductInfo);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			// 在校验时用
			/*
			 * AdRateProductInfo adRateProductInfo2 = new AdRateProductInfo();
			 * adRateProductInfo2.setProductId("xxx");
			 * adRateProductInfoList.add(adRateProductInfo2);
			 * 
			 * Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
			 * "校验接口返回");
			 */

			verifyDb(order.getOrderDetailPoList());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_004() {
		Logger.start(true, "广告费率为0");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();

			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(BigDecimal.ZERO);

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderInfo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_005() {
		Logger.start(true, "订单未付款");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(BigDecimal.ZERO);

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			// verifyDb(Arrays.asList(orderInfo));

			Logger.verifyEquals(0, new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(generateAdRateInfoReq.getOrderId()).size(),
					"不生成广告结算");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_006() {
		Logger.start(true, "订单号为0");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);
			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(BigDecimal.ZERO);

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(0l);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			// verifyDb(Arrays.asList(orderInfo));

			Logger.verifyEquals(0, new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(orderId).size(), "不生成广告结算");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_007() {
		Logger.start(true, "订单号为不存在");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(BigDecimal.ZERO);

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(1111111l);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			Logger.verifyEquals(0, new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(orderId).size(), "不生成广告结算");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_008() {
		Logger.start(true, "重复通知生成广告费用信息");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = new BigDecimal(1 + new Random().nextInt(30));

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(0.0545));

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Thread.sleep(1000);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderInfo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_009() {
		Logger.start(true, "订单商品的广告费用超过最高广告费用");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = new BigDecimal(2);

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(1.1));

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderInfo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_010() {
		Logger.start(true, "订单商品的广告费率为0");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = BigDecimal.ONE;

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(BigDecimal.ZERO);

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderInfo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_011() {
		Logger.start(true, "订单商品的广告费率为null");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = BigDecimal.ONE;

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			// adRateProductInfo.setAdRate(new BigDecimal(1.1));

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			// verifyDb(Arrays.asList(orderInfo));

			List<AdRateInfoPo> adRateInfoPoList = new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(orderId);

			Logger.verifyEquals(0, adRateInfoPoList.size(), "校验广告费率信息记录数为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_012() {
		Logger.start(true, "订单商品的最高费率为0");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			BigDecimal adCostMax = BigDecimal.ZERO;

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(1.1));

			adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(Arrays.asList(orderInfo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_013() {
		Logger.start(true, "订单商品的最高费率为null");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			Order order = placeOrderResp.getOrderList().get(0);
			long orderId = order.getOrderId();
			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();
			OrderDetailPo orderInfo = orderInfoList.get(0);

			// BigDecimal adCostMax = BigDecimal.ZERO;

			AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

			adRateProductInfo.setProductId(orderInfo.getProductId());

			adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

			adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
					.toString());

			adRateProductInfo.setAdRate(new BigDecimal(1.1));

			// adRateProductInfo.setAdCostMax(adCostMax);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(Arrays
					.asList(adRateProductInfo));

			generateAdRateInfoCall.setData(generateAdRateInfoReq);
			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			List<AdRateInfoPo> adRateInfoPoList = new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(orderId);

			Logger.verifyEquals(0, adRateInfoPoList.size(), "校验广告费率信息记录数为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_014() {
		Logger.start(true, "新交易,校验广告费率信息生成成功");
		try {

			TradingServiceResp placeOrderResp = SettlementService
					.placeOrder4(true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(new BigDecimal(new Random()
						.nextFloat() / 10));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			// verifyDb(orderInfoList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_015() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额大于广告费，校验广告费率信息生成成功");
		try {

			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4,
					RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.ZERO, false, 0, Arrays
							.asList(sellerRebateRatio.subtract(new BigDecimal(
									"0.001")), sellerRebateRatio));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());

			Thread.sleep(5000);

			// 生成安利结算单
			SettlementService.generateAnliSettlement(orderId);

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio
						.subtract(new BigDecimal("0.002")));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(orderInfoList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_016() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额小于广告费，校验广告费率信息生成成功");
		try {

			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4,
					RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = TradingPlaceAnliOrderCallService
					.placeAnlihuiOrder(BigDecimal.ZERO, false, 0, Arrays
							.asList(sellerRebateRatio.add(new BigDecimal(
									"0.001")), sellerRebateRatio));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());

			Thread.sleep(5000);

			// 生成安利结算单
			SettlementService.generateAnliSettlement(orderId);

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio
						.add(new BigDecimal("0.002")));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(orderInfoList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GenerateAdRateInfo_017() {
		Logger.start(true, "新交易安利会返点订单,买手返点金额等于广告费，校验广告费率信息生成成功");
		try {

			BigDecimal sellerRebateRatio = new BigDecimal(
					new Random().nextFloat() / 10).setScale(4,
					RoundingMode.DOWN);

			TradingServiceResp placeOrderResp = SettlementService
					.placeAnlihuiOrderExcludeFreight(Arrays.asList(
							sellerRebateRatio,
							sellerRebateRatio.add(new BigDecimal("0.001"))));

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(placeOrderResp.getUserId(), payAmount,
					placeOrderResp.getTradingId());

			Thread.sleep(5000);

			// 生成安利结算单
			SettlementService.generateAnliSettlement(orderId);

			List<OrderDetailPo> orderInfoList = placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(sellerRebateRatio);

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);
			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			Logger.verifyEquals("ok", generateAdRateInfoCall.getReturnData(),
					"校验接口返回");

			verifyDb(orderInfoList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
